package top.chatgqt.SmartChargeStation.netty.server.handlers;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import top.chatgqt.SmartChargeStation.netty.server.utils.HTTPUtils;
import top.chatgqt.SmartChargeStation.protocol.mqtt.message.ChargePayload;
import top.chatgqt.SmartChargeStation.protocol.protobuf.ChargingCmdProtobuf;

/**
 * 处理Protobuf反序列化之后的数据
 */
@Slf4j
public class ProtobufHandler extends SimpleChannelInboundHandler<ChargingCmdProtobuf.ChargingCmd> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChargingCmdProtobuf.ChargingCmd msg) throws Exception {
        log.info(">>>> 微信小程序发送的指令: " + msg.getCmd());

        byte cmdByte = (byte) msg.getCmd().charAt(0);
        log.info(">>>> 充电桩指令转化为byte: " + cmdByte);

        // 采用OKHTTP类库去调用mqtt客户端模块的API
        ChargePayload payload = new ChargePayload();
        payload.setCmd(cmdByte);

        // 发送
        HTTPUtils.doPOST(payload);
    }
}
